package com.rysima.wujiansima.demo.homework;

import java.io.*;
import java.util.ArrayList;
import java.util.function.Predicate;

/*
分析以下需求，并用代码实现

项目根路径下有个questions.txt文件内容如下：

5+5

150-25

155*155

2555/5

要求：读取内容计算出结果，将结果写入到results.txt文件中

5+5=10

//....

思考:如果说读取questions.txt文件的内容,算出计算结果,再写入questions.txt文件,即读和写的操作时针对同一个文件,应该如何操作

 */
public class HomeWork5 {
    public static void main(String[] args) throws IOException {
//        要求：读取内容计算出结果，将结果写入到results.txt文件中
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\任意的爹\\Desktop\\questions.txt"));

        ArrayList<String> list = new ArrayList<>();
        String s4 ="";
        while ((s4 = br.readLine()) != null){

            list.add(s4);
        }
        list.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                if (s.length() < 2){
                    return true;
                }
                return false;

            }
        });
        for (String s : list) {
            System.out.println(s);
        }

        String s = list.get(0);
        String[] st = s.split("\\+");
        int j = Integer.parseInt(st[0]);
        int j1 = Integer.parseInt(st[1]);
        list.set(0,j + " + " + j1 + "=" + (j + j1));
        String s1 = list.get(1);
        String[] st1 = s1.split("-");
        int a = Integer.parseInt(st1[0]);
        int a1 = Integer.parseInt(st1[1]);
        int i1 =a -a1;
        list.set(1,a + " - " + a1 + "=" + i1);
        String s2 = list.get(2);
        String[] st2 = s2.split("\\*");
        int q= Integer.parseInt(st2[0]);
        int q1 = Integer.parseInt(st2[1]);
        int i2 = q * q1;
        list.set(2,q + " * " + q1 + "=" + i2);
        String s3 = list.get(3);
        String[] st3 = s3.split("/");
        int w = Integer.parseInt(st3[0]);
        int w1 = Integer.parseInt(st3[1]);
        int i3 = w / w1;
        list.set(3,w + " / " + w1 + "=" + i3);
        /*System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);*/
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\任意的爹\\Desktop\\questions.txt"));
        for (String s5 : list) {
            bw.write(s5);
            bw.newLine();
        }
        br.close();
        bw.close();



    }
}
